package model.util;

import java.util.List;

import model.market.MarketInformation;
import model.market.MarketConfig.VolatilityAdjustment;

public class SharpeCalc {

	
	public static double calculateSharpeRatio(double[] weightings, double[] expectedExcessAssetReturns,MarketInformation info,double indexVariance,double[] assetNonSpecVariance) {
		double expectedExcessReturn = 0;
		for(int i=0;i<weightings.length;i++){
			expectedExcessReturn+=weightings[i]*expectedExcessAssetReturns[i];
		}
		
		
		double sharpeRatio = expectedExcessReturn/calculatePortfolioStdDev(weightings, expectedExcessAssetReturns,info,indexVariance,assetNonSpecVariance);
		return sharpeRatio;
	}
	
	public static double calculatePortfolioStdDev(double[] weightings, double[] expectedExcessAssetReturns,MarketInformation info, double indexVariance,double[] assetNonSpecVariance){
		double portfolioBeta = 0;
		double portfolioNonSpecVariance =0;
		
		
		//AMDO: working here.. something fishy going on with vol. Vol jump causes step back after upping on 100% asset 0 boost. Fixing vol needs more observation.
		//AMXX: wokring hee.. binding in asset non spec variance. is it a waste of time and should be stripped out?
		for(int i=0;i<weightings.length;i++){
			portfolioBeta+=weightings[i]*info.getBeta(i);
			
			double thisAssetNonSpecVariance = 0;
			if(assetNonSpecVariance==null){
				thisAssetNonSpecVariance=info.getAssetNonSpecificVariance(i);
			}else{
				thisAssetNonSpecVariance=assetNonSpecVariance[i];
			}
			
			portfolioNonSpecVariance += weightings[i]*weightings[i]*thisAssetNonSpecVariance;
			
		}
		
		indexVariance = indexVariance==0?info.getIndexVariance().get(info.getIndexVariance().size()-1):indexVariance;
		
		double portfolioVariance = portfolioBeta*portfolioBeta*indexVariance+portfolioNonSpecVariance;
		double portfolioVol = Math.sqrt(portfolioVariance);
		
		if(info.getConfig().getVolatilityAdjustment().equals(VolatilityAdjustment.ADJUSTED)){
			portfolioVol=(portfolioVol+.3)/2;
		}else if(info.getConfig().getVolatilityAdjustment().equals(VolatilityAdjustment.FIXED)){
			portfolioVol=.3;
		}
		
		return portfolioVol;
	}
}
